<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller {
	function __construct(){
		parent::__construct();
	}

	function index(){
		//Get truck post
		//if(!$data = $this->cache->get("post_home")){
			$this->load->model("post_model","postModel");
			$this->load->model('images_model','imagesModel');
			$this->load->model("category_post_model","categoryPostModel");
			$this->load->model("category_model","categoryModel");
			$this->load->model("group_model","groupModel");
			$this->load->model('type_model','typeModel');
			$where['where']['status'] = 1;
			$data["allPosts"]["total"] = $this->postModel->count_rows($where);
			$where["limit"] = 20;
			$where["order"] = "update_date DESC";
			$data["allPosts"]["list"] = $this->postModel->get($where);

			$this->cache->write($data,"post_home",0);
		//}

		//Get All post
		$this->_assign("allPosts",$data["allPosts"]["list"]);
		$this->_assign("totalPost",$data["allPosts"]["total"]);

		PageUtil::addVar("title",$this->config->item("slogan","site")." - ".$this->config->item("sitename","site"));
		//Fetch
		$this->_fetch("index");
	}

	function admin_index(){
		if(!$this->ion_auth->in_group("admin")){
			show_404();
		}
		$this->_fetch("admin_index");
	}

	private function _getPostByCategoryID($category_id = NULL, $limit = 20) {
		if(empty($category_id)){
			return false;
		}
		$where["where_in"]["cat_id"] = $category_id;
		$where["limit"] = $limit;
		$where["group"] = "pos_id";
		$where["order"] = "pos_update_date DESC";
		$where['where']['status'] = 1;
		$posts = $this->categoryPostModel->get($where);
		unset($where["limit"]);
		$returnPost["total"] = $this->categoryPostModel->count_rows($where);
		$returnPost["list"] = "";
		if(!empty($posts)){
			foreach ($posts as $key => $value) {
				$returnPost["list"][$key] = $value;
				$returnPost["list"][$key]["categories"] = array_reverse(Util::getCategoryInherit($value["cat_id"]));
				$returnPost["list"][$key]["province"] = Util::getProvince($posts[$key]["province"]);
				$returnPost["list"][$key]["type"] = Util::getType($posts[$key]["typ_id"]);
				$returnPost["list"][$key]["images"] = $this->imagesModel->get(array("where"=>array("pos_id"=>$value["id"])));
				$returnPost["list"][$key]["meta"] = Util::getPostMeta($posts[$key]["id"]);
				$returnPost["list"][$key]["comment_count"] = Util::getCommentCount($posts[$key]["typ_id"]);
			}
		}
		return $returnPost;
	}

	function admin_clearcache(){
		if(!CIUser::checkPermission("admin")){
			show_404();
		}
		$this->cache->delete_all();
	}

}